<?php


/**
 * 取得上次的过滤条件
 * @param   string  $param_str  参数字符串，由list函数的参数组成
 * @return  如果有，返回array('filter' => $filter, 'sql' => $sql)；否则返回false

function get_filter($param_str = '')
{
    $filterfile = basename(PHP_SELF, '.php');
    if ($param_str)
    {
        $filterfile .= $param_str;
    }
    if (isset($_GET['uselastfilter']) && isset($_COOKIE['ECSCP']['lastfilterfile'])
        && $_COOKIE['ECSCP']['lastfilterfile'] == sprintf('%X', crc32($filterfile)))
    {
        return array(
            'filter' => unserialize(urldecode($_COOKIE['ECSCP']['lastfilter'])),
            'sql'    => base64_decode($_COOKIE['ECSCP']['lastfiltersql'])
        );
    }
    else
    {
        return false;
    }
}
*/


/**
 * 保存过滤条件
 * @param   array   $filter     过滤条件
 * @param   string  $sql        查询语句
 * @param   string  $param_str  参数字符串，由list函数的参数组成

function set_filter($filter, $sql, $param_str = '')
{
    $filterfile = basename(PHP_SELF, '.php');
    if ($param_str)
    {
        $filterfile .= $param_str;
    }
    setcookie('ECSCP[lastfilterfile]', sprintf('%X', crc32($filterfile)), time() + 600);
    setcookie('ECSCP[lastfilter]',     urlencode(serialize($filter)), time() + 600);
    setcookie('ECSCP[lastfiltersql]',  base64_encode($sql), time() + 600);
}
*/

/**
 * 获得所有商品类型
 *
 * @access  public
 * @return  array
 */
function get_goodstype()
{
	//get_filter();
        /* 记录总数以及页数 */
   	$sql 	= 	"SELECT COUNT(*) FROM " . 'ybr_goods_type' ;
    /* 查询记录 */
    $sql 	= 	"SELECT t.*, COUNT(a.cat_id) AS attr_count ".
	           	"FROM " . 'ybr_goods_type' . " AS t ".
	           	"LEFT JOIN ". 'ybr_attribute' . " AS a ON a.cat_id=t.cat_id ".
	           	"GROUP BY t.cat_id ";
	           	//'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];

    //set_filter($filter, $sql);
	$all = M()->query($sql);

    foreach ($all AS $key=>$val)
    {
        $all[$key]['attr_group'] = strtr($val['attr_group'], array("\r" => '', "\n" => ", "));
    }
    return $all;
}


/**
 * 获得指定的商品类型的详情
 *
 * @param   integer     $cat_id 分类ID
 *
 * @return  array
 */
function get_goodstype_info($cat_id)
{
	$CommonModel	=	new CommonModel();
    $sql 			= 	"SELECT * FROM " . 'ybr_goods_type' . " WHERE cat_id='$cat_id'";
    return $CommonModel->getRow($sql);
}

/**
 * 更新属性的分组
 *
 * @param   integer     $cat_id     商品类型ID
 * @param   integer     $old_group
 * @param   integer     $new_group
 *
 * @return  void
 */
function update_attribute_group($cat_id, $old_group, $new_group)
{
    $sql 	= 	"UPDATE " . 'ybr_attribute' .
            	" SET attr_group='$new_group' WHERE cat_id='$cat_id' AND attr_group='$old_group'";
    M()->query($sql);
}

/**
 * 获得指定的商品类型下所有的属性分组
 *
 * @param   integer     $cat_id     商品类型ID
 *
 * @return  array
 */
function get_attr_groups($cat_id){
	$CommonModel	=	new CommonModel();	
    $sql 			= 	"SELECT attr_group FROM " . 'ybr_goods_type' . " WHERE cat_id='$cat_id'";
    $grp 			= 	str_replace("\r", '', $CommonModel->getOne($sql));

    if ($grp){
        return explode("\n", $grp);
    }
    else{
        return array();
    }
}

/**
 * 自动返回表名字,兼容带有前缀的,自动去除前缀
 * @param	$tableName 
 * @example	order_info
 * @return	OrderInfo
 * 
 */
//function auto_table_name($table){
//	/*判断是否存在前缀*/
//	$is_exsist_prefix	=	strstr($table,"ybr_"); 
//	if($is_exsist_prefix){
//		$table			=	str_replace('ybr_', '', $table);
//	}
//		
//	$table_name_array	=	explode("_", $table);
//	$tableName			=	'';
//	if(count($table_name_array) > 1){
//		foreach($table_name_array as $name){
//			$tableName 	.=	ucwords($name);
//		}
//	}else{
//		$tableName	=	ucwords( $table_name_array[0]);
//	}
//	return $tableName;
//}

/**
 * 截取UTF-8编码下字符串的函数
 *
 * @param   string      $str        被截取的字符串
 * @param   int         $length     截取的长度
 * @param   bool        $append     是否附加省略号
 *
 * @return  string
 */
function sub_str($str, $length = 0, $append = true){
    $str = trim($str);
    $strlength = strlen($str);

    if ($length == 0 || $length >= $strlength){
        return $str;
    }
    elseif ($length < 0){
        $length = $strlength + $length;
        if ($length < 0){
            $length = $strlength;
        }
    }

    if (function_exists('mb_substr')){
        $newstr = mb_substr($str, 0, $length, EC_CHARSET);
    }
    elseif (function_exists('iconv_substr')){
        $newstr = iconv_substr($str, 0, $length, EC_CHARSET);
    }
    else{
        //$newstr = trim_right(substr($str, 0, $length));
        $newstr = substr($str, 0, $length);
    }

    if ($append && $str != $newstr){
        $newstr .= '...';
    }

    return $newstr;
}












